from http import HTTPStatus

import pytest


def test_create_client_parking_route(client):
    # Create test client and parking
    client.post(
        "/clients/",
        json={
            "name": "Ivan",
            "surname": "Ivanov",
            "credit_card": "1234123412341234",
            "car_number": "A000AA98",
        },
    )
    client.post(
        "/parkings/",
        json={
            "address": "Nevskiy pr., 1",
            "opened": True,
            "count_places": 456,
            "count_available_places": 123,
        },
    )

    result = client.post(
        "/client_parkings/",
        json={
            "client_id": 1,
            "parking_id": 1,
        },
    )
    assert result.status_code == HTTPStatus.CREATED
    assert result.json.get("time_in") is not None
    assert result.json.get("time_out") is None


def test_create_client_parking_route_duplicate(client):
    result = client.post(
        "/client_parkings/",
        json={
            "client_id": 1,
            "parking_id": 1,
        },
    )
    assert result.status_code == HTTPStatus.BAD_REQUEST


@pytest.mark.parametrize(
    "wrong_json",
    [
        {
            "client_id": 1,
            "parking_id": 123,
        },
        {
            "client_id": 123,
            "parking_id": 123,
        },
        {
            "client_id": 123,
            "parking_id": 1,
        },
    ],
)
def test_create_client_parking_route_wrong_ids(client, wrong_json):
    result = client.post(
        "/client_parkings/",
        json=wrong_json,
    )
    assert result.status_code == HTTPStatus.NOT_FOUND


@pytest.mark.parametrize(
    "wrong_json",
    [
        {
            "client_id": 1,
            "parking_id": None,
        },
        {
            "client_id": None,
            "parking_id": None,
        },
        {
            "client_id": None,
            "parking_id": 1,
        },
    ],
)
def test_create_client_parking_route_none_ids(client, wrong_json):
    result = client.post(
        "/client_parkings/",
        json=wrong_json,
    )
    assert result.status_code == HTTPStatus.BAD_REQUEST


def test_create_client_parking_route_no_places_available(client):
    # Create test parking with no free places
    parking = client.post(
        "/parkings/",
        json={
            "address": "Nevskiy pr., 2",
            "opened": True,
            "count_places": 100,
            "count_available_places": 0,
        },
    )

    result = client.post(
        "/client_parkings/",
        json={
            "client_id": 1,
            "parking_id": parking.json.get("id"),
        },
    )
    assert result.status_code == HTTPStatus.BAD_REQUEST
    assert "not available" in result.json.get("error")


def test_finish_client_parking_route(client):
    result = client.delete("/client_parkings/", json={"client_id": 1, "parking_id": 1})
    assert result.status_code == HTTPStatus.NO_CONTENT


@pytest.mark.parametrize(
    "wrong_json",
    [
        {
            "client_id": 1,
            "parking_id": 123,
        },
        {
            "client_id": 123,
            "parking_id": 123,
        },
        {
            "client_id": 123,
            "parking_id": 1,
        },
    ],
)
def test_finish_client_parking_route_with_wrong_id(client, wrong_json):
    result = client.delete("/client_parkings/", json=wrong_json)
    assert result.status_code == HTTPStatus.NOT_FOUND


@pytest.mark.parametrize(
    "wrong_json",
    [
        {
            "client_id": 1,
            "parking_id": None,
        },
        {
            "client_id": None,
            "parking_id": None,
        },
        {
            "client_id": None,
            "parking_id": 1,
        },
    ],
)
def test_finish_client_parking_route_with_none_id(client, wrong_json):
    result = client.delete("/client_parkings/", json=wrong_json)
    assert result.status_code == HTTPStatus.BAD_REQUEST
